import 'package:flutter/material.dart';

enum AnimationType {
  rightToLeft, // 从右到左
  leftToRight, // 从左到右
  bottomToTop, // 从下到上
  topToBottom, // 从上到下
  fade, // 淡入淡出
  scale, // 缩放
  rotation // 旋转
}

class RouteAnimation {
  void animateRoute(
    BuildContext context,
    Widget page, {
    AnimationType type = AnimationType.rightToLeft,
    Duration duration = const Duration(milliseconds: 250),
    Curve curve = Curves.fastOutSlowIn, // 使用更高效的曲线
  }) {
    // 使用RepaintBoundary包装页面以优化性能
    final wrappedPage = RepaintBoundary(child: page);

    Navigator.push(
      context,
      PageRouteBuilder(
        settings: RouteSettings(name: ''),
        pageBuilder: (_, __, ___) => wrappedPage,
        transitionsBuilder: (_, animation, secondaryAnimation, child) {
          // 使用更高效的动画构建方式
          final tween = _getTweenForType(type);
          final curvedAnimation = CurvedAnimation(
            parent: animation,
            curve: curve,
          );

          return _buildOptimizedTransition(type, curvedAnimation, child, tween);
        },
        transitionDuration: duration,
        reverseTransitionDuration: duration,
        opaque: true, // 设置为不透明以提高性能
        maintainState: true,
      ),
    );
  }

  // 根据动画类型获取适当的补间动画
  dynamic _getTweenForType(AnimationType type) {
    switch (type) {
      case AnimationType.rightToLeft:
        return Tween<Offset>(begin: const Offset(1.0, 0.0), end: Offset.zero);
      case AnimationType.leftToRight:
        return Tween<Offset>(begin: const Offset(-1.0, 0.0), end: Offset.zero);
      case AnimationType.bottomToTop:
        return Tween<Offset>(begin: const Offset(0.0, 1.0), end: Offset.zero);
      case AnimationType.topToBottom:
        return Tween<Offset>(begin: const Offset(0.0, -1.0), end: Offset.zero);
      case AnimationType.fade:
        return Tween<double>(begin: 0.0, end: 1.0);
      case AnimationType.scale:
        return Tween<double>(begin: 0.95, end: 1.0);
      case AnimationType.rotation:
        return Tween<double>(begin: 0.0, end: 1.0);
    }
  }

  // 构建优化的过渡动画
  Widget _buildOptimizedTransition(AnimationType type,
      Animation<double> animation, Widget child, dynamic tween) {
    switch (type) {
      case AnimationType.rightToLeft:
      case AnimationType.leftToRight:
      case AnimationType.bottomToTop:
      case AnimationType.topToBottom:
        return SlideTransition(
          position: tween.animate(animation),
          child: child,
        );
      case AnimationType.fade:
        return FadeTransition(
          opacity: tween.animate(animation),
          child: child,
        );
      case AnimationType.scale:
        return ScaleTransition(
          scale: tween.animate(animation),
          child: child,
        );
      case AnimationType.rotation:
        return RotationTransition(
          turns: tween.animate(
            CurvedAnimation(
              parent: animation,
              curve: const Interval(0.0, 0.5),
            ),
          ),
          child: FadeTransition(
            opacity: animation,
            child: child,
          ),
        );
    }
  }
}
